package com.ygq.Impl;

import DO.StudentDO;
import DO.WorkTestTwoDataDO;
import DTO.SubmitStudentScoreDTO;
import VO.UserMsgVO;
import VO.WorkMsgVO;
import VO.WorkScoreVO;
import VO.WorksVO;
import com.ygq.GradesService;
import mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import result.Result;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class GradesServiceImpl implements GradesService {

    @Autowired
    private WorkClassMapper workClassMapper;

    @Autowired
    private WorkMapper workMapper;

    @Autowired
    private WorkScoreMapper workScoreMapper;

    @Autowired
    private studentMapper studentMapper;

    @Autowired
    private WorkFileUploadMapper workFileUploadMapper;

    @Autowired
    private WorkTestTwoDataMapper workTestTwoDataMapper;


    // 获取班级信息
    public List<WorksVO> getWorks(Integer classId) {
        // 第一步，查询wakeClass表，查询出该班级的wakeId
        List<Integer> wakeIdList = workClassMapper.getWakeIdByClassId(classId);
        if(wakeIdList.isEmpty()){
            // 表示这个班级暂时没有作业，需要报异常然后
            return new ArrayList<>();
        }
        return workMapper.getWokesByWakeIdList(wakeIdList);
    }


    public List<WorkScoreVO> getWorkScores(Integer classId, Integer workId) {
        List<WorkScoreVO> workScoreVOList = new ArrayList<>();
        // 1. 根据班级id查询出班级的学生状态
        List<StudentDO> studentDOList = studentMapper.getAllById(classId);
        for (StudentDO studentDO : studentDOList) {
            WorkScoreVO workScoreVO = new WorkScoreVO();
            workScoreVO.setUserId(studentDO.getId());
            workScoreVO.setUsername(studentDO.getUsername());
            workScoreVO.setStudentNumber(studentDO.getStudentNumber());
            Double score = workScoreMapper.selectScoreByUserIdWorkId(studentDO.getId(),workId);
            workScoreVO.setScore(score);
            workScoreVO.setScoreState(score==null?0:1);
            workScoreVOList.add(workScoreVO);
        }
        // 2. 利用学生id和这个workId查询出学生状态
        return workScoreVOList;
    }

    // 获取结束时间
    public LocalDateTime getEndtime(Integer classId, Integer workId) {
        return workClassMapper.getEndTimeByClassId(classId,workId);
    }


    public List<Integer> getUserIds(Integer classId) {
        return studentMapper.getUserIds(classId);
    }

    public UserMsgVO getUserMsg(Integer userId) {
        return studentMapper.getUserMsg(userId);
    }

    // 获取题目数据
    public WorkMsgVO getWorkmsg(Integer workId) {
        WorkMsgVO workMsgVO = new WorkMsgVO();
        workMsgVO.setScores(workFileUploadMapper.getScoreByWorkId(workId));
        workMsgVO.setQuestion(workFileUploadMapper.getQuestionByWorkId(workId));
        return workMsgVO;
    }


    public String getAddress(Integer userId, Integer workId, Integer order) {
        return workTestTwoDataMapper.getAddress(userId,workId,order);
    }


    @Transactional
    public Integer submitStudentScore(SubmitStudentScoreDTO submitStudentScoreDTO) {
        // 查询在workScore表中是否存在数据
        Double scoreById = workScoreMapper.getScoreById(submitStudentScoreDTO.getWorkId(), submitStudentScoreDTO.getUserId());
        if (scoreById!=null){
            // 表示已经提交过成绩
            return 0;
        }

        // 先向 work test two data表中修改分数数据 ，注意判断有一些用户可能没有提交
        double scores = 0.0;
        int k = 1;
        for (Double score : submitStudentScoreDTO.getScores()) {
            scores = scores +score;
            if(workTestTwoDataMapper.uptateScore(k,score,submitStudentScoreDTO.getWorkId(),submitStudentScoreDTO.getUserId()) ==0){
                // 表示用户并没有提交这个题目的文件，按照0分计算，同时插入自己的数据
                WorkTestTwoDataDO workTestTwoDataDO = new WorkTestTwoDataDO();
                workTestTwoDataDO.setScore(score);
                workTestTwoDataDO.setWorkId(submitStudentScoreDTO.getWorkId());
                workTestTwoDataDO.setUserId(submitStudentScoreDTO.getUserId());
                workTestTwoDataDO.setAddress("0");// 0表示未提交
                workTestTwoDataDO.setOrder(k);
                workTestTwoDataMapper.save1(workTestTwoDataDO);
            }
            k++;
        }
        // 在向workSCore表示插入总分数据
        workScoreMapper.save(scores,submitStudentScoreDTO.getUserId(),submitStudentScoreDTO.getWorkId());
        return 1;
    }


    public List<Double> getStudentScore(Integer userId, Integer workId) {
        // 获取到学生在这个试卷的每道题的得分分布情况
        List<Double> scores = workTestTwoDataMapper.getScores(userId, workId);
        // 获取学生到底提没提交分数
        Double scoreById = workScoreMapper.getScoreById(workId, userId);
        if (scoreById==null){
            // 表示没有在 work_score 查询到分数，表示还没有写入分数
            if (scores.isEmpty()){
                // 没有提交
                scores.add(-1.0);
            }
            // 表示未批改,将scores的第一个修改未-1，前端好分辨
            scores.set(0,-1.0);
        }
        return scores;
    }

    @Transactional
    public Integer updateStudentScore(SubmitStudentScoreDTO submitStudentScoreDTO) {
        // 查询在workScore表中是否存在数据
        Double scoreById = workScoreMapper.getScoreById(submitStudentScoreDTO.getWorkId(), submitStudentScoreDTO.getUserId());
        if (scoreById==null){
            // 表示没有提交
            return 0;
        }
        // 先向 work test two data表中修改分数数据
        double scores = 0.0;
        int k = 1;
        for (Double score : submitStudentScoreDTO.getScores()) {
            scores = scores +score;
            workTestTwoDataMapper.uptateScore(k,score,submitStudentScoreDTO.getWorkId(),submitStudentScoreDTO.getUserId());
            k++;
        }
        // 在向workSCore表示插入总分数据
        workScoreMapper.update(scores,submitStudentScoreDTO.getUserId(),submitStudentScoreDTO.getWorkId());
        return 1;
    }


}
